package com.app.shell.model
{
	import com.app.common.GlobalConstants;
	import com.app.shell.message.ShellMessage;
	import com.app.utils.NamedObjectMap;
	
	import flash.system.ApplicationDomain;
	
	import mx.events.ModuleEvent;
	import mx.modules.IModule;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	
	public class ActiveWidgetProxy {

		[MessageDispatcher] public var dispatcher:Function;
		
		//private static const MODULE_URL:String = 'org/robotlegs/demos/acmewidgetfactory/modules/widget/WidgetModule.swf';
		private static var MODULE_URL:String;
		
		protected var widgetMap:NamedObjectMap;
		protected var infoMap:NamedObjectMap;
		protected var loadedModuleInfo:IModuleInfo;
		
		public function ActiveWidgetProxy() {
			widgetMap = new NamedObjectMap();
			infoMap = new NamedObjectMap();
		}

		public function createWidget(id:String):void
		{
			if (widgetMap.hasKey(id) == false)
			{
				loadedModuleInfo = null;
				loadModule(id);
			}
		}
		
		public function getWidget(id:String):*
		{
			return widgetMap.getObject(id);
		}
		
		public function getWidgetId(widget:IModule):String
		{
			return widgetMap.getKey(widget);
		}
		
		public function hasWidgetId(id:String):Boolean
		{
			return widgetMap.hasKey(id);
		}
		
		public function removeById(id:String):void
		{
			widgetMap.removeByKey(id);
		}

		protected function createModule(id:String):void
		{
			var widget:IModule = loadedModuleInfo.factory.create() as IModule;
			//dispatcher(new ShellMessage(GlobalConstants.REGISTER_MODULE_ROUTER_ADDRESS, widget));
			widgetMap.registerObject(widget, id);
			if(id != GlobalConstants.MODULE_LOGGER)
				dispatcher(new ShellMessage(GlobalConstants.WIDGET_CREATED, id));
			else
				dispatcher(new ShellMessage(GlobalConstants.CREATE_LOGGER_COMPLETE, id));
		}
		
		protected function loadModule(id:String):void
		{
			if (loadedModuleInfo == null)
			{
				MODULE_URL = id + '.swf';
				var info:IModuleInfo = ModuleManager.getModule(MODULE_URL);
				info.data = id;
				info.addEventListener(ModuleEvent.PROGRESS, onModuleProgress);
				info.addEventListener(ModuleEvent.READY, onModuleReady);
				info.addEventListener(ModuleEvent.ERROR, onModuleError);
				//http://old.nabble.com/Module-unloads-but-FlexModuleFactory-instance-does-not-unload-td15784905.html
				//info.load(new ApplicationDomain(ApplicationDomain.currentDomain));
				info.load();
				infoMap.registerObject(info, id);
				widgetMap.registerObject(info, id);
			}
			else
			{
				createModule(id);
			}
		}
		
		protected function onModuleReady(e:ModuleEvent):void
		{
			var widgetId:String = infoMap.getKey(e.module);
			loadedModuleInfo = e.module;
			dispatcher(new ShellMessage(GlobalConstants.LOG_MESSAGE, 'Widget Id: ' +widgetId));
			infoMap.removeByObject(e.module);
			createModule(widgetId);
		}
		
		protected function onModuleError(e:ModuleEvent):void
		{
			var widgetId:String = infoMap.getKey(e.module);
			dispatcher(new ShellMessage(GlobalConstants.LOG_MESSAGE, 'Widget Id: ' + widgetId + ' ' +e.errorText));
		}
		
		protected function onModuleProgress(e:ModuleEvent):void
		{
			var widgetId:String = infoMap.getKey(e.module);
			dispatcher(new ShellMessage(GlobalConstants.LOG_MESSAGE, 'Widget Id: ' + widgetId + ' ' +e.bytesLoaded + '/' + e.bytesTotal + ' bytes loaded'));
		}

	}
}